# -*- coding: utf-8 -*-
"""
@Time ： 2020/10/30 20:55
@Auth ： Mr. William 1052949192
@Company ：特斯汀学院 @testingedu.com.cn
@Function ：Web自动化关键字库
"""
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from verify.Slide import Slide
from verify.verify_pic import Verify


class Web:

    def __init__(self):
        """
        构造函数，创建driver的实例变量
        """
        self.driver = None
        # self.driver = webdriver.Chrome()
        # 标识当前打开的是什么浏览器
        self.br = 'gc'
        self.ver = 'None'

    def openbrowser(self, br='gc'):
        """
        打开浏览器的关键字
        :param br: gc(chrome);ff(firefox);ie(Ie);
        :return: 是否成功
        """
        if br == 'gc':
            self.driver = webdriver.Chrome()
        elif br == 'ff':
            self.driver = webdriver.Firefox()
        elif br == 'ie':
            self.driver = webdriver.Ie()
        else:
            print('error：%s浏览器暂未支持' % br)
            return False

        self.br = br
        # 最大化
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)

    def geturl(self, url="None"):
        """
        打开网站
        :param url: 需要打开的网址，必须是标准的url
        :return: 是否成功
        """
        if url.startswith('http') or url.startswith('https'):
            self.driver.get(url)
        else:
            print('error：Url(%s)不合法' % url)
            return False

    def __find_ele(self, locator=''):
        """
        通过selenium去找元素
        :param locator: 定位参数，支持id，xpath，css
        :return: 返回找到的元素
        """
        if locator.startswith('/'):
            ele = self.driver.find_element_by_xpath(locator)
        elif locator.startswith('#') or locator.__contains__('>'):
            ele = self.driver.find_element_by_css_selector(locator)
        else:
            ele = self.driver.find_element_by_id(locator)

        return ele

    def input(self, locator='None', value='None'):
        """
        找到元素，并完成输入
        :param locator: 定位参数
        :param value: 需要输入的值
        :return: 是否输入成功
        """
        ele = self.__find_ele(locator)
        ele.send_keys(str(value))

    def click(self, locator='None'):
        """
        找到元素并完成点击，IE使用js点击
        :param locator: 定位参数
        :return:
        """
        ele = self.__find_ele(locator)
        if self.br == 'ie':
            # ie使用js点击
            self.driver.execute_script("arguments[0].click()", ele)
        else:
            ele.click()

    def intoiframe(self,locator):
        """
        进入iframe
        :param locator: iframe的定位器
        :return:
        """
        ele = self.__find_ele(locator)
        self.driver.switch_to.frame(ele)

    def outiframe(self):
        """
        退出所有iframe
        :return:
        """
        self.driver.switch_to.default_content()

    def hover(self, locator='None'):
        """
        找到元素，鼠标悬停到该元素
        :param locator: 定位参数
        :return: 是否成功
        """
        if self.br == 'ie' or self.br == 'ff':
            ele = self.__find_ele(locator)
            x = ele.location.get('x')
            y = ele.location.get('y')
            self.executejs('window.scrollTo(%d,%d)' % (int(x), int(y) - 100))

        time.sleep(1)
        ele = self.__find_ele(locator)
        # 创建一个action对象
        action = ActionChains(self.driver)
        action.move_to_element(ele).perform()

    def executejs(self, js='None'):
        """
        执行js脚本
        :param js: 需要执行的js语句
        :return: 执行结果
        """
        self.driver.execute_script(js)

    def sleep(self, t='1'):
        """
        固定等待
        :param t: 需要等待的时间，单位是s
        :return: 是否成功
        """
        t = int(t)
        time.sleep(t)

    def gettext(self, locator='None'):
        """
        获取到元素的文本
        :param locator: 定位参数
        :return: 获取到的文本
        """
        ele = self.__find_ele(locator)
        text = ele.text
        return text

    def verify(self, locator='None',im='../verify/imgs/verify.png'):
        """
        图文识别，获取验证码图片截图，并识别文字
        :param locator: 验证码图片元素
        :return: 识别的验证码文字
        """
        ele = self.__find_ele(locator)
        # 截图
        ele.screenshot(im)
        # 识别验证码
        # 初始化一个对象
        ver = Verify('wuqingfqng', '6e8ebd2e301f3d5331e1e230ff3f3ca5', '904357')
        verify_text = ver.PostPic(1004, im=im)
        self.ver = verify_text
        return verify_text

    def slide(self, target='None', template='None'):
        """
        滑动验证码识别和滑动实现
        :return: 是否成功
        """
        slide_verify = Slide(self.driver)
        slide_verify.slide(target,template)

    def quit(self):
        """
        退出浏览器
        :return: 是否成功
        """
        self.driver.quit()
